"""
Case Type   : 系统视图PG_PREPARED_STATEMENT
Case Name   : 超级用户查询函数
Create At   : 2023/12/4
Owner       : ningyali
Description :
    1.建表
    2.新建session1，创建预备语句
    3.新建session2，创建预备语句
    4.创建超级用户
    5.使用超级用户连接数据库，查询视图
    6.清理环境
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功
    4.执行成功
    5..执行成功，可查询到stmt1,stmt2
    6.执行成功
History     :
"""

import os
import time
import unittest

from yat.test import macro

from testcase.utils.ComThread import ComThread
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class PgPreparedStatement21(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        text = f'-----{os.path.basename(__file__)} start-----'
        self.log.info(text)
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.t_name = 't_prepare_21'
        self.prepare_name = 'stmt'
        self.u_name = 'u_prepare_21'
        self.prepare_sql = f"prepare {self.prepare_name} as " \
            f"insert into {self.t_name} values(1, 'openGauss');" \
            f"select pg_sleep(3);"
        self.query_sql = 'select * from pg_prepared_statement(0) ' \
                         'order by 1, 2;'

    def test_1(self):
        text = 'step1:建表; expect:执行成功'
        self.log.info(f'-----{text}-----')
        sql = f'drop table if exists {self.t_name};' \
            f'create table {self.t_name}(c1 int, c2 text);'
        self.log.info(sql)
        res = self.pri_sh.execut_db_sql(sql)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res, f'执行失败：{text}')

        text = 'step2:新建session1，创建预备语句; expect:执行成功'
        self.log.info(f'-----{text}-----')
        session1 = ComThread(self.pri_sh.execut_db_sql,
                             args=(f'{self.prepare_sql}',))
        session1.setDaemon(True)
        session1.start()

        text = 'step3:新建session2，创建预备语句; expect:执行成功'
        self.log.info(f'-----{text}-----')
        session2 = ComThread(self.pri_sh.execut_db_sql,
                             args=(f'{self.prepare_sql}',))
        session2.setDaemon(True)
        session2.start()
        time.sleep(1)

        text = 'step4:创建超级用户; expect:执行成功'
        self.log.info(f'-----{text}-----')
        sql = f'drop user if exists {self.u_name};' \
            f'create user {self.u_name} with sysadmin ' \
            f'password \\"{macro.COMMON_PASSWD}\\";'
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, res,
                      f'执行失败：{text}')

        text = 'step5:使用超级用户连接数据库，查询视图; ' \
               'expect:执行成功，可查询到stmt1,stmt2'
        self.log.info(f'-----{text}-----')
        cmd = f'source {macro.DB_ENV_PATH};' \
            f'gsql -d {self.pri_sh.node.db_name} -U {self.u_name} ' \
            f'-W "{macro.COMMON_PASSWD}" -c "{self.query_sql}"'
        self.log.info(cmd)
        res = self.pri_sh.node.sh(cmd).result()
        self.log.info(res)
        self.assertEqual(res.count(self.prepare_name), 4, f'执行失败：{text}')

        text = '获取session1、session2结果'
        self.log.info(f'-----{text}-----')
        session1.join(180)
        session2.join(180)
        res1 = session1.get_result()
        res2 = session2.get_result()
        self.log.info(f'res1:\n{res1}')
        self.log.info(f'res2:\n{res2}')
        self.assertIn('PREPARE', res1, f'执行失败：{text}')
        self.assertIn('PREPARE', res2, f'执行失败：{text}')

    def tearDown(self):
        self.log.info('----run teardown----')
        text1 = 'step6:清理环境; expect:执行成功'
        self.log.info(f'-----{text1}-----')
        sql = f'drop table if exists {self.t_name} cascade;' \
            f'drop user if exists {self.u_name};'
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res, f'执行失败：{text1}')
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, res,
                      f'执行失败：{text1}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
